<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
/*
	[天生创想] (C)2008-2011 515158 Inc.
	This is NOT a freeware, use is subject to license terms
    URL:http://www.515158.com/
*/

empty($do) && $do = 'list';
if ($do == 'list') {
	//列表信息 
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = 20;
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac=index&fileurl=registration';
   //时间
	$vstartdate = getGP('vstartdate','G');
	$venddate = getGP('venddate','G');
	if ($vstartdate!='' && $venddate!='') {
	$wheresql .= " AND (date>='".$vstartdate."' and date<='".$venddate."')";
	$url .= '&vstartdate='.$vstartdate.'&venddate='.$venddate;
	}
	//员工
	if(!is_superadmin() && !check_purview("registration_"))
   {
	if ($user = getGP('user','G','int')) {
		$wheresql .= " AND uid = $user";
		$url .= '&user='.$user;	
	}
	}else{
	$wheresql .= " AND uid = $_USER->id";
	}
	//状态
	if ($startyype = getGP('startyype','G')) {
		$wheresql .= " AND startyype ='".$startyype."'";
		$url .= '&startyype='.$startyype;	
	}
	if ($endtype = getGP('endtype','G')) {
		$wheresql .= " AND endtype ='".$endtype."'";
		$url .= '&endtype='.$endtype;	
	}
	
	
	
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."registration  WHERE 1 $wheresql");
     $sql = "SELECT * FROM ".DB_TABLEPRE."registration WHERE 1 $wheresql ORDER BY id desc LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);

	include_once('template/index.php');

} elseif ($do == '删 除') {
get_config_keytable("registration_");
	$idarr = getGP('id','P','array');
	foreach ($idarr as $id) {
	$db->query("DELETE FROM ".DB_TABLEPRE."registration WHERE id = '$id'");
	if($id!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => $id,
		'title' => '删除考勤信息'.$id,
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => '7'
	);
	insert_db('oalog',$oalog);
	}	
	}
	
	goto_page('admin.php?ac=index&fileurl=registration');

} elseif ($do == '导 出') {
get_config_keytable("registration_excel");
//导出excel
//生成格式
$datename="registration".get_date('Y',PHP_TIME).get_date('m',PHP_TIME).get_date('d',PHP_TIME).get_date('H',PHP_TIME).get_date('i',PHP_TIME).get_date('s',PHP_TIME);
$outputFileName = 'excel/'.$datename.'.xls';
//生成数据
    $content = array();
	$archive=array("姓名","上班时间","下班时间","状态(上班/下班)","时长(迟到/早退)","IP(上班/下班)");
	$content[] = $archive;
	$wheresql = '';
	//根据条件导出
	//时间
	$vstartdate = getGP('vstartdate','P');
	$venddate = getGP('venddate','P');
	if ($vstartdate!='' && $venddate!='') {
	$wheresql .= " AND (date>='".$vstartdate."' and date<='".$venddate."')";
	}
	//员工
	if (check_purview('istration_registration')=='1')
   {
	if ($user = getGP('user','P','int')) {
		$wheresql .= " AND uid = $user";
		$url .= '&user='.$user;	
	}
	}else{
	$wheresql .= " AND uid = $_USER->id";
	}
	//状态
	if ($startyype = getGP('startyype','P')) {
		$wheresql .= " AND startyype ='".$startyype."'";
	}
	if ($endtype = getGP('endtype','P')) {
		$wheresql .= " AND endtype ='".$endtype."'";	
	}
	//echo getGP('keyword','P');
	//exit;
	//SQL查询要导出的内容
	//$sql = "SELECT * FROM ".DB_TABLEPRE."registration WHERE $wheresql ORDER BY id desc";
	$sql = "SELECT * FROM ".DB_TABLEPRE."registration WHERE 1 $wheresql ORDER BY id DESC";
	$result = $db->query($sql);
	while ($row = $db->fetch_array($result)) {	
	//判断日期
	//if ($row[birthdate]!=''){
	//$birthdate=explode('-',$row[birthdate]);
	//$birthdate_day=explode(' ',$birthdate[2]);
	//$birthdate_data=$birthdate[0].".".$birthdate[1].".".$birthdate_day[0];
	//}
	//将数据传递给数组
	$archive = array("".$row[name]."","".":".$row['startdate']."","".":".$row['enddate']."","".$row['startyype']."/".$row['endtype']."","".$row[starthour]."小时/".$row['endhour']."小时"."","".$row['startip']."/".$row['endip']."");
	//初使化数组数据
	$content[] = $archive;
	}
//$myArr=$content;
$excel = new ExcelWriter($outputFileName);
if($excel==false) 
echo $excel->error; 
foreach($content as $v){
$excel->writeLine($v);
}
$excel->sendfile($outputFileName);
} 


?>